﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AStar.Collections;
using AStar.Class;

namespace AStar
{
    class TestDataGen
    {
        Random rnd = new Random();
        int width;

        public int Width
        {
            get { return width; }
            set { width = value; }
        }
        int height;

        public int Height
        {
            get { return height; }
            set { height = value; }
        }
        int offset = 15;

        public int Offset
        {
            get { return offset; }
            set { offset = value; }
        }

        int maxLength = 300;

        public int MaxLength
        {
            get { return maxLength; }
            set { maxLength = value; }
        }
        MyList<City> cities = new MyList<City>();

        public TestDataGen()
        { }

        public LogicClass.Logic Generate(int amount)
        {
            LogicClass.Logic re = new LogicClass.Logic();

            GenerateCities(re, amount);
            for (int i = 0; i < cities.Count(); i++)
            {
                City destination = cities.Get(i);
                City a = null;
                City b = null;
                OpenList<City> oL = new OpenList<City>();
                for (int j = 0; j < cities.Count(); j++)
                {
                    City temp = new City(cities.Get(j).Name,cities.Get(j).X,cities.Get(j).Y);
                    temp.Estimate(destination);
                    //oL.Add(temp);
                    if (!destination.Name.Equals(cities.Get(j).Name))
                    {
                        
                        
                        if (a == null)
                        {
                            a = temp;
                        }
                        else if (b == null)
                        {
                            if (a.MaxLength > temp.MaxLength)
                            {
                                b = a;
                                a = temp;
                            }
                            else
                            {
                                b = temp;
                            }
                        }
                        else if (a.MaxLength > temp.MaxLength)
                        {
                            b = a;
                            a = temp;
                        }
                        else if (b.MaxLength > temp.MaxLength)
                        {
                            b = temp;
                        }
                    }
                }
                //re.AddStreet(cities.Get(i), oL.Get(0),rnd.Next(300));
                //re.AddStreet(cities.Get(i), oL.Get(1), rnd.Next(300));
                re.AddStreet(cities.Get(i), a, rnd.Next(maxLength));
                re.AddStreet(cities.Get(i), b, rnd.Next(maxLength));
            }
            for (int i = 0; i < cities.Count(); i++)
            {
                re.Cities.Add(cities.Get(i));
            }
            return re;
        }

        private void GenerateCities(LogicClass.Logic re, int amount)
        {
            Int32[,] cords = new Int32[amount, 2];
            int added = 0;
            int x = 0;
            int y = 0;
            while (added < cords.GetLength(0))
            {
                x = rnd.Next(width);
                y = rnd.Next(height);
                bool exists = false;
                for (int i = 0; i < cords.GetLength(0); i++)
                {
                    if (cords[i, 0] <= x + offset && cords[i, 0] >= x - offset)
                    {
                        if (cords[i, 1] <= y + offset && cords[i, 1] >= y - offset)
                        {
                            exists = true;
                            break;
                        }
                    }
                }
                if (!exists)
                {
                    //23,2
                    cords[added, 0] = x;
                    cords[added, 1] = y;
                    added++;
                    cities.Add(new City("City" + (added), x, y));
                }
            }
        }
    }
}
